﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class frmConfiguration : Form
    {
        #region Properties
        private frmMain MdiForm { get; set; }
        #endregion

        #region Constructors
        public frmConfiguration(frmMain pMdiForm)
        {
            InitializeComponent();
            this.MdiForm = pMdiForm;
        }
        #endregion

        #region Events
        private void frmConfiguration_Load(object sender, EventArgs e)
        {
            this.MdiForm.Event_OnTranslationChanged += new frmMain.OnTranslationChangedHandler(Translate);
            Translate();
            cboDatabase.Items.Add(new clsItem("Microsoft SQL Server 2000", clsSettings.enumDatabaseChoice.MicrosoftSQLServer2000));
            cboDatabase.Items.Add(new clsItem("MySQL Server", clsSettings.enumDatabaseChoice.MySQLServer));
            FillInputs();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckInputs())
            {
                if (TestInputs())
                {
                    if (SaveInputs())
                    {
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Saving not possible.");
                    }
                }
                else
                {
                    MessageBox.Show("No connection to the database.");
                }
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (CheckInputs())
            {
                if (TestInputs())
                {
                    MessageBox.Show("The connection test was successful.");
                }
                else
                {
                    MessageBox.Show("The connection test was not successful.");
                }
            }
        }
        #endregion

        #region Public methods
        #endregion

        #region Private methods
        /// <summary>
        /// Translates every visible element.
        /// </summary>
        private void Translate()
        {
            this.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, "Text", this.Text, 0);
            grpPreferences.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpPreferences.Name, grpPreferences.Text, 0);
            lblDatabase.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblDatabase.Name, lblDatabase.Text, 0);
            lblAddress.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblAddress.Name, lblAddress.Text, 0);
            lblDatabaseName.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblDatabaseName.Name, lblDatabaseName.Text, 0);
            lblUserName.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblUserName.Name, lblUserName.Text, 0);
            lblPassword.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblPassword.Name, lblPassword.Text, 0);
            grpOptions.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpOptions.Name, grpOptions.Text, 0);
            btnCancel.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnCancel.Name, btnCancel.Text, 0);
            btnSave.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnSave.Name, btnSave.Text, 0);
            btnTest.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnTest.Name, btnTest.Text, 0);
        }

        /// <summary>
        /// Checks if inputs can be read or not.
        /// </summary>
        /// <returns>The state.</returns>
        private Boolean InputsAvailable()
        {
            if (this.MdiForm.DataAccess.Settings.DatabaseAddress == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Fills the fields with the given inputs.
        /// </summary>
        private void FillInputs()
        {
            if (InputsAvailable())
            {
                for (Int32 i = 0; i < cboDatabase.Items.Count; i++)
                {
                    if (cboDatabase.Items[i].ToString().Replace(" ", "") == this.MdiForm.DataAccess.Settings.DatabaseChoice.ToString())
                    {
                        cboDatabase.SelectedIndex = i;
                        break;
                    }
                }
                txtAddress.Text = this.MdiForm.DataAccess.Settings.DatabaseAddress;
                txtDatabaseName.Text = this.MdiForm.DataAccess.Settings.DatabaseName;
                txtUserName.Text = this.MdiForm.DataAccess.Settings.UserName_Database;
                txtPassword.Text = this.MdiForm.DataAccess.Settings.Password_Database;
            }
        }

        /// <summary>
        /// Checks the existence of inputs in all the fields.
        /// </summary>
        /// <returns></returns>
        private Boolean CheckInputs()
        {
            Boolean CheckStatus = true;

            if (cboDatabase.SelectedIndex < 0)
            {
                ChangeLabelTextColor(lblDatabase, true);
                CheckStatus = false;
            }
            else
            {
                ChangeLabelTextColor(lblDatabase, false);
            }

            if (txtAddress.Text == "")
            {
                ChangeLabelTextColor(lblAddress, true);
                CheckStatus = false;
            }
            else
            {
                ChangeLabelTextColor(lblAddress, false);
            }

            return CheckStatus;
        }

        /// <summary>
        /// Does a connection test.
        /// </summary>
        /// <returns>The status of the test.</returns>
        private Boolean TestInputs()
        {
            return this.MdiForm.DataAccess.DatabaseConnectable(GetDatabaseSelection(), txtAddress.Text, txtUserName.Text, txtPassword.Text, txtDatabaseName.Text);
        }

        /// <summary>
        /// Saves the inputs to the configuration file.
        /// </summary>
        /// <returns>The success of the saving.</returns>
        private Boolean SaveInputs()
        {
            clsSettings.enumDatabaseChoice DatabaseSelection = GetDatabaseSelection();
            return this.MdiForm.DataAccess.Settings.SaveSettings(DatabaseSelection, txtAddress.Text, txtUserName.Text, txtPassword.Text, txtDatabaseName.Text);
        }

        /// <summary>
        /// Gets the selected database.
        /// </summary>
        /// <returns>The selected database.</returns>
        private clsSettings.enumDatabaseChoice GetDatabaseSelection()
        {
            if (cboDatabase.Text.Replace(" ", "") == clsSettings.enumDatabaseChoice.MicrosoftSQLServer2000.ToString())
            {
                return clsSettings.enumDatabaseChoice.MicrosoftSQLServer2000;
            }
            else if (cboDatabase.Text.Replace(" ", "") == clsSettings.enumDatabaseChoice.MySQLServer.ToString())
            {
                return clsSettings.enumDatabaseChoice.MySQLServer;
            }
            else
            {
                throw new ArgumentException("Error 2");
            }
        }

        /// <summary>
        /// Changes the color of the label text to red or black.
        /// </summary>
        /// <param name="pLabel">The label.</param>
        /// <param name="Red">Change to red with true, change to black with false.</param>
        private void ChangeLabelTextColor(Label pLabel, Boolean Red)
        {
            if (Red)
            {
                if (pLabel.ForeColor != Color.Red)
                {
                    pLabel.ForeColor = Color.Red;
                }
            }
            else
            {
                if (pLabel.ForeColor != Color.Black)
                {
                    pLabel.ForeColor = Color.Black;
                }
            }
        }
        #endregion
    }
}
